//
//  PreviewView.swift
//  WatermarkCamera
//
//  Created by AxcLogo on 4/7/2024.
//

import UIKit

// MARK: - [PreviewView.FillMode]

extension PreviewView {
    /// 填充模式
    enum FillMode: UInt {
        case stretch = 0

        case preserveAspectRatio = 1

        case preserveAspectRatioAndFill = 2
    }
}

// MARK: - [PreviewView]

extension PreviewView {
    /// 设置填充模式
    /// - Parameter fillMode: 填充模式
    func setFillMode(_ fillMode: PreviewView.FillMode) {
        gpuImageView.fillMode = .init(rawValue: fillMode.rawValue)!
    }

    /// 获取填充模式
    func fillMode() -> FillMode {
        .init(rawValue: gpuImageView.fillMode.rawValue)!
    }
}

// MARK: - [PreviewView]

class PreviewView: UIView {
    // Lifecycle

    convenience init() {
        self.init(frame: .zero)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        createUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) 未实现")
    }

    // Open

    override func layoutSubviews() {
        super.layoutSubviews()

        gpuImageView.frame = bounds
    }

    func createUI() {
        addSubview(gpuImageView)
    }

    // Internal

    lazy var gpuImageView: GPUImageView = {
        let view = GPUImageView()

        view.backgroundColor = .black

        return view
    }()
}

// MARK: OutputProtocol

extension PreviewView: OutputProtocol {
    func setOutput(_ output: GPUImageOutput) {
        output.addTarget(gpuImageView)
    }
}
